﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SimpleLotterySystem
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            






            int[] groupNo = new int[int.Parse(textBox1.Text)];
            int maxSameAllow = int.Parse(textBox2.Text);
            Random rd = new Random();
            string res = "";

            int numOfProj = int.Parse(textBox1.Text) / maxSameAllow;
            if (int.Parse(textBox1.Text) % maxSameAllow != 0) numOfProj++;
            int[] projNo = new int[numOfProj];
            for (int i = 0; i < projNo.Length; i++) projNo[i] = maxSameAllow;

            for (int i = 0; i < groupNo.Length; i++)
            {
                int j = rd.Next(projNo.Length);              //在所有项目中随机抽取一个项目
                if (projNo[j] == 0) { i--; continue; }       //如果随机到的项目剩余名额已经为0，则重新尝试为该组分配一个项目
                groupNo[i] = j;                              //如果随机到的项目仍有剩余名额，则配合该组至抽到的项目
                projNo[j]--;                                 //该项目可被抽取到的次数-1
                //MessageBox.Show("第" + (i + 1) + "组\t" + "第" + (groupNo[i] + 1) + "个项目\n");
                res += "第"+(i+1)+"组\t"+"第"+(groupNo[i]+1)+"个项目\n";
            }
            


            MessageBox.Show("共有"+ int.Parse(textBox1.Text) + "个小组\n最多允许"+ maxSameAllow + "个小组相同项目\n" + "共计需要" + numOfProj + "个项目\n\n" + res);
        }
    }
}
